package tools;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import sun.io.Converters;

import com.sun.org.apache.xpath.internal.operations.Bool;

import fr.afcepf.atod11.framework.forms.LoginForm;
import fr.afcepf.atod11.framework.forms.LoginForm;
import fr.afcepf.atod11.framework.forms.interfaces.IActionForm;


public  class MyBeanPopulate {
	
	public static void populateBeanAnna(IActionForm form, Map params) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
System.out.println("on passe dans le populate bean anna");
		if(params!=null){
			Class formClass = form.getClass();
			Iterator<String> it = params.keySet().iterator();
			
			while(it.hasNext()){
				String key = it.next();
				String[] values = (String[])params.get(key);
				char begin = key.charAt(0);
				String beginUpper = String.valueOf(begin);
				beginUpper = beginUpper.toUpperCase();
				String key2= key.substring(1);
				String key3 = String.valueOf(beginUpper)+key2;
				Method method;
				Method[] methods = formClass.getDeclaredMethods();
				for(int i =0; i<methods.length; i++){
					if(methods[i].getName().equals("set"+key3)){						
						@SuppressWarnings("all")
						Class[] classes = methods[i].getParameterTypes();
						System.out.println(classes[0].getName());
						Class type = classes[0];
						//type.getc
						
//Object o = classe.newInstance();
						methods[i].invoke(form,  type.cast(values[0]));
					}
				}
//				if(((String []) params.get(key))[0]=="True")
//				method = formClass.getMethod("set"+key3,  boolean.class);
//				else
//					method = formClass.getMethod("set"+key3,  String.class);
//				System.out.println(((Object []) params.get(key))[0]);
//				method.invoke(form, ((String []) params.get(key))[0]);
			//	System.out.println(((LoginForm)form).getEmail());
			}
	}
	}
/***
 * deprecated
 * @param form
 * @param params
 */
	@SuppressWarnings("all")
	public static void populateBeanWilfried(IActionForm form, Map params) {

		Class classform = form.getClass();

		for (Object key : params.keySet()) {
			Object[] values = new Object[1];
			values[0]= ((Object[])params.get(key))[0];

			String keyString = key.toString();
			String mySetmethodName = "set"
					+ keyString.substring(0, 1).toUpperCase()
					+ keyString.substring(1);
			String myGetmethodName = "get"
					+ keyString.substring(0, 1).toUpperCase()
					+ keyString.substring(1);
			System.out.println(mySetmethodName);
			try {
				System.out.println(key);
				Method myGetMethod = classform.getMethod(myGetmethodName);
				Class type = myGetMethod.getReturnType();
				
				System.out.println(type.getName());
				if(type.getName().equals("int")) {
					int i = Integer.parseInt( values[0].toString());
					values[0] = i;
				}
				System.out.println(values[0]);
				Method setMethod = classform.getMethod(mySetmethodName, type);
				setMethod.invoke(form, values[0]);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private static Class wrapperPrimitiveToClass(Class type) {
		Map<String, Class> builtInMap = new HashMap<String, Class>();
		
		builtInMap.put("int", Integer.TYPE);
		builtInMap.put("long", Long.TYPE);
		builtInMap.put("double", Double.TYPE);
		builtInMap.put("float", Float.TYPE);
		builtInMap.put("bool", Boolean.TYPE);
		builtInMap.put("char", Character.TYPE);
		builtInMap.put("byte", Byte.TYPE);
		builtInMap.put("shord", Short.TYPE);
		
		
		return builtInMap.get(type.getName());
	}

	

}
